﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using System.Reflection;

namespace CodeDomTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Microsoft.CSharp.CSharpCodeProvider cscp;
            //Microsoft.VisualBasic.VBCodeProvider vbcp;
            //Microsoft.JScript.JScriptCodeProvider jscp;

            StringBuilder sb = new StringBuilder();

            foreach (CompilerInfo ci in System.CodeDom.Compiler.CodeDomProvider.GetAllCompilerInfo())
            {
                try
                {
                    try
                    {
                        sb.AppendLine(ci.CodeDomProviderType.AssemblyQualifiedName);
                    }
                    catch (Exception exception)
                    {
                        sb.AppendLine(exception.Message);
                    }

                    foreach (string lng in ci.GetLanguages())
                    {
                        sb.AppendLine("lang: " + lng);
                    }
                    sb.AppendLine("---");

                }
                catch (Exception exception)
                {
                    sb.AppendLine(exception.Message);
                }
            }

            textBox1.Text = sb.ToString();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "using System;\r\n\r\npublic class Test\r\n{\r\n  public Test() {}\r\n  public string Metoda(string input)\r\n  {\r\n    return input;\r\n  }\r\n}\r\n\r\n";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
            CompilerParameters compilerparams = new CompilerParameters();
            compilerparams.IncludeDebugInformation = false;
            compilerparams.GenerateExecutable = false;
            compilerparams.GenerateInMemory = true;
            compilerparams.ReferencedAssemblies.Add("system.dll");
            compilerparams.ReferencedAssemblies.Add("system.data.dll");
            compilerparams.ReferencedAssemblies.Add("npgsql.dll");
            //compilerparams.MainClass = "Test";
            CompilerResults results = provider.CompileAssemblyFromSource(compilerparams, textBox1.Text);

            StringBuilder sb = new StringBuilder();

            if (results.Errors.Count != 0)
            {
                foreach (CompilerError err in results.Errors)
                {
                    sb.AppendLine(err.ToString());
                }
            }
            else
            {
                sb.AppendLine("/* COMPILED! \r\n ");
                foreach (AssemblyName assembly in results.CompiledAssembly.GetReferencedAssemblies())
                {
                    sb.AppendLine(assembly.ToString());
                }
                sb.AppendLine(" */");
                Assembly asm = results.CompiledAssembly;
                object o = asm.CreateInstance("Test");

                sb.AppendLine(o.GetType().InvokeMember("Metoda", BindingFlags.InvokeMethod, null, o, new object[] { textBox1.Text }).ToString());
            }

            textBox1.Text = sb.ToString();
        }
    }
}
